<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CoWeight;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;

class CoWeightController extends AdminController
{
    protected $title = '重量管理';
    protected $school_id;
    public function __construct(Request $request)
    {
        $this->school_id = $request->get('school_id',0);
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CoWeight(), function (Grid $grid) {

            if($this->school_id>0){
                $grid->model()
                    ->where("school_id",$this->school_id)
                    ->orderBy('sort')
                    ->paginate(10);
            }else{
                $grid->model()
                    ->orderBy('sort')
                    ->paginate(10);
            }
            $grid->id->sortable();
            $grid->column('school_id','学校')->display(function ($v){
                return \App\Models\CoSchool::where('id',$v)->value('name');
            });
            $grid->name;
            $grid->price;
            $grid->status->switch();
            $grid->column('distribution_type','配送方式')->display(function ($id){
                if($id == 1){
                    return '上楼配送';
                }else{
                    return '楼下配送';
                }
            })->sortable();
            $grid->sort;
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->tools('<a href="/admin/coWeight/create?school_id='.$this->school_id.'" class="btn btn-primary btn-outline"><i class="feather icon-plus"></i><span class="d-none d-sm-inline">&nbsp;&nbsp;新增</span></a>');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CoWeight(), function (Show $show) {
            $show->id;
            $show->price;
            $show->sort;
            $show->status;
            $show->name;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CoWeight(), function (Form $form) {
            $form->hidden('id');
            if($this->school_id > 0){
                $CoSchool = new \App\Models\CoSchool();
                $choole=$CoSchool::all()->where('id',$this->school_id)->pluck('name', 'id');
                if($choole){
                    $form->select('school_id')
                        ->options($choole->toArray())->required();
                }
            }else{
                $form->select('school_id')
                    ->options(function ($school_id) {
                        $CoSchool = new \App\Models\CoSchool();
                        $data =  $CoSchool::all()->where('id',$school_id)->pluck('name', 'id');
                        return $data->toArray();
                    })
                    ->required();
            }
            $form->radio('distribution_type','配送方式')->options([1=>'上楼配送',2=>'楼下配送'])->default(1)->required();
            $form->text('name','重量')->required()
                ->help("格式: 0-1公斤");
            $form->currency('price')->symbol('￥')->required();
            $form->text('sort')->default(10);
            $form->radio('status')->options(['1'=>'启用','0'=>'禁用'])->default(1);
            $form->display('created_at');
            $form->display('updated_at');
            $form->tools(function (Form\Tools $tools) {
                // 去掉跳转列表按钮
                $tools->disableList();
                // 去掉跳转详情页按钮
                $tools->disableView();
                // 去掉删除按钮
                $tools->disableDelete();
            });
            if($this->school_id > 0){
                $form->tools('<a href="/admin/coWeight?school_id='.$this->school_id .'" class="btn btn-sm btn-white "><i class="feather icon-list"></i><span class="d-none d-sm-inline">&nbsp;列表</span></a>');
            }else{
                $form->tools('<a href="/admin/coWeight?school_id='.$this->school_id .'" class="btn btn-sm btn-white "><i class="feather icon-list"></i><span class="d-none d-sm-inline">&nbsp;列表</span></a>');
            }
            $form->footer(function ($footer) {
                // 去掉`重置`按钮
                $footer->disableReset();

                // 去掉`查看`checkbox
                $footer->disableViewCheck();

                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();

                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
